﻿#ifndef _EXCEPTIONS_HPP_
#define _EXCEPTIONS_HPP_

#include <iostream>
#include <string>
#include <sstream>
#include <exception>

namespace SimpleJson
{
    // Json 错误基类
    class JsonError : public std::exception
    {
    public:
        JsonError(void) {}
        
        const char* what() const throw() override
        {
            return msg.c_str();
        }

    protected:
        std::stringstream stream;  // 提示文本流
        std::string       msg;     // 提示文本
    };

    // Json IO 错误
    class JsonIOError : public JsonError
    {
    public:
        JsonIOError(const std::string& desc,
            int line = 0,
            int col = 0,
            bool print_pos = true)
        {
            stream << "[JsonIOError]";
            stream << desc;
            if (print_pos)
            {
                stream << " (line: " << line << " col: " << col << ")\n";
            }

            msg = stream.str();
        }
    };

    // Json 类型错误
    class JsonTypeError : public JsonError
    {
    public:
        JsonTypeError(const std::string& desc,
            const std::string& expected, const std::string& actual)
        {
            stream << "[JsonTypeError]";
            stream << desc;
            stream << "(expected: " << expected << " actual: " << actual << ")\n";
            msg = stream.str();
        }
    };

    // Json 空指针错误
    class JsonNullError : public JsonError
    {
    public:
        JsonNullError(const std::string& desc)
        {
            stream << "[JsonNullError]";
            stream << desc << "\n";
            msg = stream.str();
        }
    };

    // Json 未找到错误
    class JsonNotFoundError : public JsonError
    {
    public:
        JsonNotFoundError(const std::string& desc)
        {
            stream << "[JsonNotFoundError]";
            stream << desc << "\n";
            msg = stream.str();
        }
    };

    // Json 数组越界错误
    class JsonOutOfBoundError : public JsonError
    {
    public:
        JsonOutOfBoundError(const std::string& desc)
        {
            stream << "[JsonOutOfBoundError]";
            stream << desc << "\n";
            msg = stream.str();
        }
    };

} // namespace SimpleJson

#endif // _EXCEPTIONS_HPP_
